package com.mike.controller;

import com.mike.po.User;
import com.mike.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description
 * @Date 2021/11/29 11:06
 * @Created by mike
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 注册接⼝，true成功，false失败
     *
     * @return true/false
     */
    @GetMapping("/register/{email}/{password}/{code}")
    public Integer register(HttpServletResponse response,@PathVariable String email, @PathVariable String password, @PathVariable String code) {
        return userService.register(response,email, password, code);
    }

    /**
     * 是否已注册，根据邮箱判断,true代表已经注册过，
     * false代表尚未注册
     *
     * @return true/false
     */
    @GetMapping("/isRegistered/{email}")
    public Boolean isRegistered(@PathVariable String email) {
        return userService.isRegistered(email);
    }

    /**
     * 登录接⼝，验证⽤户名密码合法性，根据⽤户名和
     * 密码⽣成token，token存⼊数据库，并写⼊cookie
     * 中，登录成功返回邮箱地址，重定向到欢迎⻚
     *
     * @return 邮箱地址
     */
    @GetMapping("/login/{email}/{password}")
    public String login(HttpServletResponse response, @PathVariable String email, @PathVariable String password) {
        return userService.login(response, email, password);
    }

    /**
     * 根据token查询⽤户登录邮箱地址接⼝
     *
     * @return 邮箱地址
     */
    @GetMapping("/info/{token}")
    public String getEmailByToken(@PathVariable String token) {
        return userService.getEmailByToken(token);
    }

    @GetMapping("/list")
    public List<User> list(){
        return userService.list();
    }
}
